package com.hmall.search.test;

import com.hmall.common.client.ItemClient;
import com.hmall.common.dto.ItemDTO;
import com.hmall.common.dto.PageDTO;
import com.hmall.search.service.ItemSearchSerivce;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Slf4j
@SpringBootTest
@RunWith(SpringRunner.class)
public class BulkImport2Es {

    @Autowired
    private ItemClient itemClient;
    @Autowired
    private ItemSearchSerivce itemSearchSerivce;


    /**
     * 把数据导入Es
     */
    @Test
    public void import2Es(){

        int page=1,size=1000;
        while (true){
            //        分页查询商品信息
            PageDTO<ItemDTO> pageDTO = itemClient.findByPage4Search(page, size);
            if(pageDTO == null || CollectionUtils.isEmpty(pageDTO.getList())){
                break;
            }
            log.info("page={}",page);
            List<ItemDTO> list = pageDTO.getList();
            // 导入Es
            itemSearchSerivce.importIndex(list);

            if(list.size()<size){
                break;
            }
            log.info("=========================");
            page++;
        }

    }
}
